import sys
from PyQt5.QtWidgets import QApplication, QWidget, QPushButton, QVBoxLayout, QLabel
from PyQt5.QtCore import Qt
from PyQt5.QtGui import QPixmap

class FilterDemo(QWidget):
    def __init__(self):
        super().__init__()
        self.init_ui()
        
    def init_ui(self):
        layout = QVBoxLayout()
        
        self.info_label = QLabel('测试不同的文件过滤器')
        self.info_label.setAlignment(Qt.AlignCenter)
        
        # 不同的过滤器示例
        filters = [
            ("图片文件", "图片文件 (*.png *.jpg *.jpeg *.bmp *.gif)"),
            ("文档文件", "文档文件 (*.txt *.doc *.docx *.pdf)"),
            ("代码文件", "代码文件 (*.py *.cpp *.java *.js *.html *.css)"),
            ("所有文件", "所有文件 (*)")
        ]
        
        for name, filter_str in filters:
            btn = QPushButton(f'选择{name}')
            btn.clicked.connect(lambda checked, f=filter_str: self.open_with_filter(f))
            layout.addWidget(btn)
        
        layout.addWidget(self.info_label)
        self.setLayout(layout)
        self.setWindowTitle('文件过滤器演示')
        
    def open_with_filter(self, filter_string):
        file_name, selected_filter = QFileDialog.getOpenFileName(
            self, "选择文件", "", filter_string
        )
        
        if file_name:
            self.info_label.setText(f"文件: {file_name}\n过滤器: {selected_filter}")



            
if __name__ == "__main__":
    app = QApplication(sys.argv)
    window = FilterDemo()
    window.show()
    sys.exit(app.exec_())